import { Injectable } from '@nestjs/common';
import { CreateProjectDto } from './dto/create-project.dto';
import { UpdateProjectDto } from './dto/update-project.dto';
import { PrismaService } from '@/prisma/prisma.service';

@Injectable()
export class ProjectService {
  constructor(private prisma: PrismaService) {}
  async create(dto: CreateProjectDto, req: any) {
    //添加项目
    const project = await this.prisma.project.create({
      data: {
        name: dto.name,
        userId: req.user.id,
      },
    });
    return project;
  }
  //查找所有项目
  async findAll(req: any) {
    return await this.prisma.project.findMany({
      where: {
        userId: req.user.id,
      },
    });
  }
  //查看某个项目
  async findOne(id: number, req: any) {
    return await this.prisma.project.findFirst({
      where: {
        id,
      },
    });
  }
  //修改某个项目
  async update(id: number, dto: UpdateProjectDto, req: any) {
    const project = await this.prisma.project.update({
      where: {
        id,
      },
      data: {
        name: dto.name,
      },
    });
    return project;
  }
  //删除项目
  async remove(id: number) {
    return await this.prisma.project.delete({
      where: {
        id,
      },
    });
  }
}
